import {
    observable,
    action
} from "mobx";
import * as api from '../../utils/api'
import axios from '../../utils/http'

class kaQuan {
    @observable
    tabData = []

    @observable
    myFrom = {
        cvmKw: '', //关键字
        cvmCc: '', //商品名称
        goodsId: '', //商品Id
        cvmCfv: '', //卡券面值
        cvmCnl: 0, //卡券长度
        cvmCnp: '', //卡券密码
        cvmCvp: 0, //卡券前缀
        kaQuanDate: [], //日期
        num: 0,
        cvmStatus: 0, //卡券激活状态
        cvmGq: 0, //卡券生成数量
        cvmRn: '', //卡券名称
        isUpdate: false
    }
    @action
    insertStart = (data, setData) => {
        console.log(data, setData.target.value)
        if (data === 'cvmKw') this.myFrom.cvmKw = setData.target.value //修改关键字
        else if (data === 'cvmCc') this.myFrom.cvmCc = setData.target.value //修改商品name
        else if (data === 'cvmCfv') this.myFrom.cvmCfv = setData.target.value //修改面值
        else if (data === 'cvmCnl') this.myFrom.cvmCnl = setData.target.value //修改长度
        else if (data === 'cvmCnp') this.myFrom.cvmCnp = setData.target.value //修改卡券密码
        else if (data === 'cvmCvp') this.myFrom.cvmCvp = setData.target.value //修改卡券前缀----------------
        else if (data === 'kaQuanDate') this.myFrom.cradPwd = setData //修改日期
        else if (data === 'cvmStatus') this.myFrom.cvmStatus = setData.target.value //修改卡券激活状态
        else if (data === 'cvmGq') this.myFrom.cvmGq = setData.target.value //修改卡券生成数量
        else if (data === 'GoodcvmRnsId') this.cvmRn = setData.target.value
        else if (data === 'update') this.myFrom = {
            cvmCfv: setData.cvmCfv, //面值
            cvmCnl: setData.cvmCnl, //长度
            cvmCnp: setData.cvmCnp, //密码
            cvmCvp: setData.cvmCvp, //前缀
            cvmGq: setData.cvmGq, //卡券数量
            cvmKw: setData.cvmKw, //关键字
            cvmRn: setData.cvmGq, //卡券名称
            cvmStatus: setData.cvmStatus, //激活状态
            goodsId: setData.goodsId,
        }
        else if (data === 'insert') this.myFrom = {
            cvmKw: '', //关键字
            cvmCc: '', //商品名称
            goodsId: '', //商品Id
            cvmCfv: '', //卡券面值
            cvmCnl: 0, //卡券长度
            cvmCnp: '', //卡券密码
            cvmCvp: 0, //卡券前缀
            kaQuanDate: [], //日期
            num: 0,
            cvmStatus: 0, //卡券激活状态
            cvmGq: 0, //卡券生成数量
            cvmRn: '', //卡券名称
            isUpdate: false
        }
    }

    @observable
    goodslist = []

    @action //查询所有卡券
    insertTabData() {
        axios({
            url: api.GET_ALL_KAQUAN,
            method: 'get'
        }).then(data => {
            console.log(data)
            if (data.data.code === 200) {
                this.tabData = data.data.data
            }
        })
    }

    @action //添加或修改
    insertData() {
        let cvmEd = new Date(this.myFrom.kaQuanDate[0])
        let cvmEdf = new Date(this.myFrom.kaQuanDate[1])
        console.log({
            // cvmCc:this.myFrom.cvmCc,//名称
            cvmCfv: this.myFrom.cvmCfv, //面值
            cvmCnl: this.myFrom.cvmCnl, //长度
            cvmCnp: this.myFrom.cvmCnp, //密码
            cvmCvp: this.myFrom.cvmCvp, //前缀
            cvmGq: this.myFrom.cvmGq, //卡券数量
            cvmKw: this.myFrom.cvmKw, //关键字
            cvmRn: this.myFrom.cvmRn, //卡券名称
            cvmStatus: this.myFrom.cvmStatus, //激活状态
            goodsId: this.myFrom.goodsId,
            cvmEd,
            cvmEdf
        })
        axios({
            url: api.INSERTKAQUAN,
            method: 'post',
            data: {
                // cvmCc:this.myFrom.cvmCc,//名称
                cvmCfv: this.myFrom.cvmCfv, //面值
                cvmCnl: this.myFrom.cvmCnl, //长度
                cvmCnp: this.myFrom.cvmCnp, //密码
                cvmCvp: this.myFrom.cvmCvp, //前缀
                cvmGq: this.myFrom.cvmGq, //卡券数量
                cvmKw: this.myFrom.cvmKw, //关键字
                cvmRn: this.myFrom.cvmRn, //卡券名称
                cvmStatus: this.myFrom.cvmStatus, //激活状态
                goodsId: this.myFrom.goodsId,
                cvmEd,
                cvmEdf
            }
        }).then(data => {
            console.log(data)
        })
    }

    @action //查询所有商品
    selGoods() {
        console.log(1)
        axios({
            url: api.GET_ALL_SHANGPIN,
            method: 'get'
        }).then(data => {
            console.log(data)
            this.goodslist = data.data.data.map(item => {
                item.key = item.goodsId
                return item
            })
        })
    }

}

export default kaQuan